/*
in/out:
tail = (tail+1)%MAXSIZE
head = (head+1)%MAXSIZE

full:
(tail+1)%QUEUESIZE == head
empty:
head==tail

*/
#include <bits/stdc++.h>
using namespace std;
#define MAXSIZE 1000
vector<string> vq(MAXSIZE),nq(MAXSIZE);
int vhead,vtail,nhead,ntail;
int M;
int vsize,nsize;
bool isFull(vector<string> q,int qhead,int qtail,int qsize){
	if (qsize==0) return false; //避免%0出错 
	//	cout<<"qtail:"<<qtail<<" size:"<<qsize<<"qhead:"<<qhead<<endl;
  if ((qtail+1)%MAXSIZE==qhead){
    return true;
  }else return false;
}
void show(vector<string> q,int qhead,int qtail){
   while(qhead!=qtail){
     cout<<q[qhead]<<endl;
     qhead = (qhead+1)%MAXSIZE; //head+1才是元素 
   }
}
int main()
{
 // freopen("data.txt","r",stdin);
    cin>>M;
    while(M--){
     string op;cin>>op;
        if (op=="IN"){
            string name,type;
            cin>>name>>type;
            
            if (type=="V"){
              if (isFull(vq,vhead,vtail,vsize)){
                cout<<"队列已满，拒绝插入"<<endl;
                continue;
              }
              
                vq[vtail] = name;
                vtail = (vtail + 1) % MAXSIZE; 
                vsize++;
            }else{
              if (isFull(nq,nhead,ntail,nsize)){
                cout<<"队列已满，拒绝插入"<<endl;
                continue;
              }
             
                nq[ntail] = name;
                 ntail = (ntail + 1)%MAXSIZE;
                nsize++;
            }
            
        }else{
            string type;
            cin>>type;
            if (type=="V"){
                vhead = (vhead+1)%MAXSIZE;
                vsize--;
            }else{
              nhead = (nhead+1)%MAXSIZE;
              nsize--;
            }
        }
        
    }
  show(vq,vhead,vtail);
  show(nq,nhead,ntail);

  return 0;
}













